package com.cal.calonecal.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cal.calonecal.common.Response;
import com.cal.calonecal.pojo.entity.Choose;
import com.cal.calonecal.pojo.entity.Problem;
import com.cal.calonecal.pojo.entity.Tag;
import com.cal.calonecal.pojo.entity.TagProblem;
import com.cal.calonecal.service.IProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 76004
 * @apiNote
 * @date 2023/6/17 13:13
 */
@RestController
@RequestMapping("/problem")
@Api("题目相关")
@CrossOrigin
public class ProblemController {
    @Autowired
    private IProblemService problemService;

    @PostMapping("/add")
    public Response<Void> add(@RequestBody Problem problem) {
        return problemService.add(problem);
    }

    @DeleteMapping("/delete")
    public Response<Void> delete(Long id) {
        return problemService.delete(id);
    }

    @PutMapping("/update")
    @ApiOperation("更新题目内容，不包括选项和标签")
    public Response<Void> update(@RequestBody Problem problem) {
        return problemService.update(problem);
    }

    @GetMapping("/select-by-user-id")
    @ApiOperation("查询用户创建的题目")
    public Response<IPage<Problem>> selectByUserId(Integer pageNum, Integer pageSize) {
        return problemService.selectByUserId(pageNum, pageSize);
    }

    @GetMapping("/select-page")
    @ApiOperation("分页查询所有题目")
    public Response<IPage<Problem>> selectPage(Integer pageNum, Integer pageSize) {
        return problemService.selectPage(pageNum, pageSize);
    }

    @GetMapping("/select-by_id")
    @ApiOperation("按照id查询题目")
    public Response<Problem> selectById(long id) {
        return problemService.selectById(id);
    }

    @GetMapping("/select-by-paper_id")
    @ApiOperation("查询某张试卷里的题目")
    public Response<List<Problem>>selectByPaperId(long paperId){
        return problemService.selectByPaperId(paperId);
    }


    @PutMapping("add-tag2problem")
    @ApiOperation("给题目加tag")
    public Response<Void> addTag2Problem(String tagName, long problemId) {
        return problemService.addTag2Problem(tagName, problemId);
    }

    @GetMapping("/select-by-tag-id")
    @ApiOperation("按照tagId筛")
    public Response<IPage<Problem>> selectByTagId(long tagId, Integer pageNum, Integer pageSize) {
        return problemService.selectByTagId(tagId, pageNum, pageSize);
    }

    @GetMapping("/select-by-name")
    @ApiOperation("按照tag名字筛")
    public Response<IPage<Problem>> selectByTagName(String tagName, Integer pageNum, Integer pageSize) {
        return problemService.selectByTagName(tagName, pageNum, pageSize);
    }

    @PostMapping("/add-choose")
    @ApiOperation("给某个题目添加选项")
    public Response addChoose(@RequestBody Choose choose){
        return problemService.addChoose(choose);
    }

    @DeleteMapping("/del-choose")
    @ApiOperation("删除某个选项")
    public Response delChoose(Long choseId){
        return problemService.delChoose(choseId);
    }

    @PostMapping("/add-tag")
    @ApiOperation("给题目加tag")
    public Response addTag(Tag tag, Long problemId){
        return problemService.addTag(tag, problemId);
    }

    @PutMapping("/del-tag")
    @ApiOperation("删掉题目tag")
    public Response delTag(String tagName, Long problemId){
        return problemService.delTag(tagName, problemId);
    }
}
